//
//  CircleContentView.swift
//  DesignCode
//
//  Created by 刘文超 on 2020/5/11.
//  Copyright © 2020 刘文超. All rights reserved.
//

import SwiftUI

struct CircleContentView: View {
    
    var colorbg = Color.gray
    
    var color1 = Color.red
    var color2 = Color.blue
    var width :CGFloat = 200
    var height :CGFloat = 200
    var progressValue:CGFloat = 38
    @Binding var showAnimation:Bool
    
    var body: some View {
        let mutipler:CGFloat = width / 60
        let progress:CGFloat = 1 - (progressValue / 100)
        return  ZStack {
            
            Circle()
                .stroke(colorbg.opacity(0.6), style: StrokeStyle(lineWidth: 8*mutipler, lineCap: .round, lineJoin: .round, miterLimit: .infinity, dash: [5,0], dashPhase: 10))
                .frame(width: width, height: height)
            
            Circle()
                .trim(from:showAnimation ? progress : 1, to: 1)
                .stroke(
                    LinearGradient(gradient: Gradient(colors: [color1,color2]), startPoint: .topTrailing, endPoint: .bottomLeading),
                    style: StrokeStyle(lineWidth: 8*mutipler, lineCap: .round, lineJoin: .round, miterLimit: .infinity, dash: [5,0], dashPhase: 10)
            )
                .frame(width: width, height: height)
                .rotationEffect(Angle(degrees: 90))
                .rotation3DEffect(Angle(degrees: 180), axis: (x: 1, y: 0, z: 0))
                .shadow(color: color2.opacity(0.31), radius: 10, x: 0, y: 5)
//                .animation(.easeInOut)
            
            
            Text("\(Int(progressValue))%")
                .font(.system(size: 14*mutipler))
                .fontWeight(.bold)
//                .onTapGesture {
//                    self.showAnimation.toggle()
//            }
        }
        
    }
}

struct CircleContentView_Previews: PreviewProvider {
    static var previews: some View {
        CircleContentView(showAnimation: .constant(true))
    }
}
